<?php
    /**
    * List all real esate 
    * this action work with all user, include guest
    *
    * SEO support
    * 
    * Support : list all project in all collection
    *           List all project in on collection
    * 
    * @author trungdt
    * @since June 25, 2011
    * 
    * URI format
    * xem_bds/xxx
    * 
    * /bds/nha_dat_tu_do/: list các bds rao vặt, tự do của thành viên
    * /bds/nha_dat_tu_do/page/xx
    * 
    * /bds/Nha_dat_dam_bao/: list các bds đảm bảo
    * /bds/Nha_dat_dam_bao/page/xxx
    * 
    * /bds/nha_dat_tu_do/biet_thu/ : bds tự do thuộc loại biệt thự
    * /bds/nha_dat_tu_do/biet_thu/page/xxx
    * 
    * /bds/Nha_dat_dam_bao/biet_thu/ 
    * /bds/Nha_dat_dam_bao/biet_thu/page/xxx/
    * 
    * /bds_noibat/
    * /bds_noibat/Nha_dat_dam_bao/
    * /bds_noibat/Nha_dat_dam_bao/biet_thu/page/xxx
    * 
    * /bds_khuvuc/Ho_chi_minh/quan_1/page/xxx
    * 
    * 
    * Change logs
    * 
    */

    if (!defined('EXPONENT')) exit('');
    global $db;

    $config=$db->selectObject("bdsmodule_config","location_data = '".serialize($loc)."'");

    if (!$config)
    {
        echo "You should configure this module first";
    }
    else
    {
        // page title
        $title="Các bất động sản";
        // set og, page title
        $head_title="Các bất động sản";
        
        $max_item=$config->max_item;
        $sql_where=" 1 ";
        // permission checking
        $have_permission=false;
        if (exponent_users_isAdmin() || exponent_permissions_check('administrate',$loc) || exponent_permissions_check('create',$loc) || exponent_permissions_check('edit',$loc))
        {
            $have_permission=true;
        }

        // get action_type, load_bds
        $action_type = "";
        $loai_bds="";
        if (isset($_REQUEST['action_type']))
        {
            $action_type=_ab_safe_html_string($_REQUEST['action_type']);
        }
        if (isset($_REQUEST['loai_bds']))
        {
            $loai_bds=_ab_safe_html_string($_REQUEST['loai_bds']);
        }
        
        // select right action type
        if ($action_type == "nha_dat_tu_do") // HACKED: TODO: Must implement better
        {
            $action_type=0;
        }
        else if ($action_type == "Nha_dat_dam_bao")
        {
            $action_type=1;
        }
        else
        {
            header("Location: /index.php");
            die();
        }
        
        // check loai_bds
        $col=$db->selectObject("bds_collections","shortname = '{$loai_bds}'");
        if (!$col)
        {
            // collection not found
            $col=-1;
        }
                
        if ($have_permission)
            $sql_where.=" AND (bds_state >=0)"; // show all
        else
            $sql_where.=" AND (bds_state = 1 )"; // only show real estate being transaction
        
        // filter by action type
        $sql_where.=" AND (bds_dambao = {$action_type})";
        
        // filter by collection id
        if (isset($col->id))
        {
            $sql_where.=" AND (col_id = {$col->id})";
        }
        
        // filter by province
        
        // filter by ward
        
        // the last condition
        $now=time();
        $sql_where.=" AND (hethan  > {$now})";

        // get all projects here
        $count_post = $db->countObjects('bds',"location_data='".serialize($loc)."'  AND {$sql_where}");

        // page split
        // cal how many posts
        $page_count=floor($count_post / $max_item);
        if ($count_post % $max_item != 0)
            $page_count++;

        // get page
        $page=1;
        if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
        if (($page<1) || ($page>$page_count)) $page=1;
        $start_pos=($page-1)*$max_item;


        $sql="SELECT {$db->prefix}bds.*, {$db->prefix}provinces.short_name pshortname, {$db->prefix}provinces.name pname, {$db->prefix}provinces_wards.shortname wshortname, {$db->prefix}provinces_wards.name wname
        FROM {$db->prefix}bds 
        LEFT JOIN {$db->prefix}provinces ON {$db->prefix}provinces.id = {$db->prefix}bds.province_id
        LEFT JOIN {$db->prefix}provinces_wards ON {$db->prefix}provinces_wards.id = {$db->prefix}bds.province_ward_id

        WHERE location_data='".serialize($loc)."' AND {$sql_where} ORDER BY bds_state ASC, featured DESC, bds_dambao DESC, created_on DESC LIMIT {$start_pos},{$max_item}
        ";

        $projects= $db->selectObjectsBySql($sql);

        $projects=bdsmodule::_bdsProcess($projects);

        // FOR TEMPLTE
        _ab_theme_action_register_title($head_title,$page,1);

        //
        $template = new template('bdsmodule',"_duan_list",$loc);
        $template->register_permissions(array('administrate','configure','edit','create','delete'),$loc);
        $template->assign('projects', $projects);
        $template->assign('page_title', $title);

        $template->assign('pages',$page_count);
        $template->assign('page',$page);
        if (!(isset($_REQUEST['flow_has_set']) && ($_REQUEST['flow_has_set']==1)))
            exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
        $template->output();
    }
?>
